<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="../inc/layui/css/layui.css">
  <link rel="stylesheet" href="../css/comm.css">
</head>

<body class="layui-layout-body">
  <header>
    <div class="layui-form" id="search-form">
      <div class="flex">
        <div class="layui-form-item ">
          <label class="layui-form-label" style="width: auto">关键字</label>
          <div class="layui-input-inline">
            <input value="" type="text" id="sword" name="sword" placeholder="输入关键字" lay-verify="required"
              autocomplete="off" class="layui-input" />
          </div>
        </div>
        <button class="layui-btn" onclick="search()" style="margin-left: 20px">搜索</button>
        <!--<button class="layui-btn" onclick="resetForm()" style="margin-left: 20px">重置</button>-->
        <button class="layui-btn" onclick="reference()" style="margin-left: 20px">刷新</button>
      </div>

    </div>
  </header>
  <table class="layui-hide" id="table" lay-filter="table"></table>
</body>
<script type="text/html" id="formUpAccount">
    <div id="form" class="layui-form">
        <div class="layui-form-item" style="margin-top: 20px;">
            <label class="layui-form-label" style="width:60px">敏感词</label>
            <div class="layui-input-inline">
                <input id="sword" name="sword" type="text" style="width:300px" autocomplete="off" placeholder="请输入敏感词"
                       class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="center" style="text-align: center;">
                <button class="layui-btn layui-btn-danger" lay-submit="" lay-filter="sub">保存</button>
                <button class="layui-btn layui-btn-primary" id="closeform">取消</button>
            </div>
        </div>
    </div>
</script>
<script type="text/javascript" src="../inc/layui/layui.js"></script>
<script type="text/javascript" src="../inc/g.js"></script>
<script type="text/javascript" src="../inc/area.js"></script>
<script type="text/html" id="seeDetail">
    <a class="layui-btn layui-btn-edit layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-del layui-btn-sm" lay-event="delete">删除</a>
</script>
<script type="text/javascript">
  //下载模板
  function downloadTemplate() {
    location.href = "../download/importSword.xls";
  }

  //批量删除
  function del() {
    var d = Comm.table.checkStatus('table'); //获取选中的数据
    if (d.data.length <= 0) {
      Comm.msg("请至少选择一条数据", 5)
      return;
    }
    Comm.confirm('确定选择删除数据', function () {
      var ids = [];
      for (var i = 0; i < d.data.length; i++) {
        ids.push(d.data[i].swordId)
      }
      //执行 ajax del 操作
      AJAX.POST("/admin/sword/batchDel", {
        swordIds: ids.join(',')
      }, function (d) {
        if (d.code == 1) {
          //刷新列表
          Comm.table.reload('table');
          Comm.msg("删除成功", 1)
        } else {
          Comm.msg(d.msg, 5)
        }
      })
    })
  }

  //新增
  function add() {
    Comm.laytpl($("#formUpAccount").html()).render({}, function (html) {
      //多窗口模式，层叠置顶
      layer.open({
        type: 1,
        title: "新增敏感词",
        area: ['400px', '200px'],
        maxmin: true,
        content: html,
        success: function (layero) {
          Comm.form.verify();
          //监听指定开关
          Comm.form.on('submit(sub)', function (data) {
            AJAX.POST("/admin/sword/add", { keyword: data.field.sword }, function (d) {
              if (d.code == 1) {
                layer.closeAll();
                layer.msg("新增成功!", {
                  icon: 1
                })
                //执行重载
                Comm.table.reload('table');
              } else {
                layer.msg(d.msg, {
                  icon: 5
                })
              }
            })
          });
          $("#closeform").click(function () {
            layer.closeAll();
          })
        },
      });
    });
  }

  //修改
  function update() {
    var d = Comm.table.checkStatus('table'); //获取选中的数据
    if (d.data.length != 1) {
      Comm.msg("请选择一条数据", 5)
      return;
    }
    var obj = d.data[0];
    Comm.laytpl($("#formUpAccount").html()).render(obj, function (html) {
      //多窗口模式，层叠置顶
      layer.open({
        type: 1,
        title: "修改敏感词",
        area: ['400px', '200px'],
        maxmin: true,
        content: html,
        success: function (layero) {
          Comm.SetData(obj, 'form');
          Comm.form.verify();
          //监听指定开关
          Comm.form.on('submit(sub)', function (data) {
            data.field.swordId = obj.swordId;
            data.field.keyword = data.field.sword;
            AJAX.POST("/admin/sword/update", data.field,
              function (
                d) {
                if (d.code == 1) {
                  layer.closeAll();
                  layer.msg("修改成功!", {
                    icon: 1
                  })
                  //执行重载
                  Comm.table.reload('table');
                } else {
                  layer.msg(d.msg, {
                    icon: 5
                  })
                }
              })
          });
          $("#closeform").click(function () {
            layer.closeAll();
          })
        },
      });
    });
  }

  //刷新
  function reference() {
    Comm.table.reload('table');
  }

  function search() {
    Comm.search();
  }

  function resetForm() {
    // $("#keyword").val("");
    $("input[name='keyword']").val("")
    Comm.form.render();
  }

  //加载页面
  function pageload() {
    layui.extend({
      pca: '../../inc/layui/lay/pca'
    })
    layui.use(['pca'], function () {
      Comm.pca = layui.pca;

    })
    //初始化列表
    Comm.table.render({
      elem: '#table',
      url: config.root + '/admin/sword/list', //列表请求接口
      where: { //接口参数
      },
      cols: [
        [{
          checkbox: true
        },
        {
          field: 'sword',
          title: '敏感词'
        },
        {
          field: 'swordId',
          title: 'id',
          hide: true
        },
          /* {
               field: "",
               title: "操作",
               toolbar: "#seeDetail",
               width: 160
           }*/
        ]
      ],
      limit: 10, //分页大小
      id: 'table',
      page: true, //是否分页
      height: 'full-' + ($(".layui-form").height() + 50), //计算table底部距离
    });

    //监听table列表里按钮点击事件
    Comm.table.on('tool(table)', function (obj) {
      if (obj.event == 'delete') {
        Comm.confirm('确定选择删除数据', function () {
          AJAX.POST('/admin/sword/batchDel', {
            swordIds: obj.data.swordId
          }, function (d) {
            if (d.code == 1) {
              layer.msg("删除成功!", {
                icon: 1
              })
              //执行重载
              Comm.table.reload('table');
            } else {
              layer.msg("删除失败!", {
                icon: 5
              })
            }
          })
        })
      }
      if (obj.event == 'edit') {

      }
    });
    //批量导入

    Comm.upload.render({
      elem: '#importSword',
      field: 'file',
      accept: 'file',
      multiple: false, //多图
      number: 2,
      url: config.ossroot, //上传接口,
      before: function (obj) {
        //上传loading
        layer.load();
      },
      done: function (d) {
        var that = this;
        layer.closeAll('loading'); //关闭loading
        Comm.confirm("确认导入?", function () {
          AJAX.POST('/admin/school/importSword', {
            key: that.data.key
          }, function (d) {
            if (d.code == 1) {
              Comm.confirm(d.msg);
              //执行重载
              Comm.table.reload('table');
            } else {
              // layer.msg(d.msg, {
              //     icon: 5
              // });
              Comm.confirm(d.msg);
            }
          })
        });
      },
      error: function () {
        //请求异常回调
        layer.closeAll('loading'); //关闭loading
      }
    });


  }
</script>

</html>